<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Tag extends Model
{
    protected $guarded = [];

    /**
     * 关联文章表.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function articles()
    {
        return $this->belongsToMany(Article::class, 'article_tags');
    }

    /**
     * 统计标签下有多少篇文章.
     *
     * @param $tagId
     *
     * @return int
     */
    public function tagCount($tagId)
    {
        return $this->articles()->where('tag_id', $tagId)->count();
    }
}
